﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TL_Cloud.Utils
{
    public class FtpHelper : RebexBase
    {
        string ftpServer = "";
        string ftpUser = "";
        string ftpPwd = "";
        int ftpPort = 21;
        bool Passive;
        public FtpHelper(string ftpServer, string ftpUser, string ftpPwd, int ftpPort,bool Passive)
        {
            this.ftpServer = ftpServer;
            this.ftpUser = ftpUser;
            this.ftpPwd = ftpPwd;
            this.ftpPort = ftpPort;
            this.Passive = Passive;
            ftpClient = new Rebex.Net.Ftp();
        }
        public FtpHelper(string ftpServer, string ftpUser, string ftpPwd, bool Passive)
        {
            this.ftpServer = ftpServer;
            this.ftpUser = ftpUser;
            this.ftpPwd = ftpPwd;
            this.ftpPort = 21;
            this.Passive = Passive;
            ftpClient = new Rebex.Net.Ftp();
        }

        public FtpHelper(string ftpServer, string ftpUser, string ftpPwd)
        {
            this.ftpServer = ftpServer;
            this.ftpUser = ftpUser;
            this.ftpPwd = ftpPwd;
            this.ftpPort = 21;
            this.Passive = true;
            ftpClient = new Rebex.Net.Ftp();
        }
        private Rebex.Net.Ftp ftpClient = null;

        /// <summary>
        /// ftp的连接
        /// </summary>
        public void Connect()
        {
            ftpClient.Connect(ftpServer, ftpPort);
            ftpClient.Login(ftpUser, ftpPwd);
            ftpClient.TransferMode = Rebex.Net.FtpTransferMode.Stream;
            ftpClient.TransferType = Rebex.Net.FtpTransferType.Binary;
            ftpClient.Passive = Passive;
        }
        /// <summary>
        /// 切换目录
        /// </summary>
        /// <param name="remotePath"></param>
        public void ChangeDirectory(string remotePath)
        {
            ftpClient.ChangeDirectoryAsync(remotePath).Start();
        }

        /// <summary>
        /// 判断远程目录是否存在
        /// </summary>
        /// <param name="remotePath">远程文件夹名称</param>
        public bool DirectoryExists(string remotePath)
        {
            return ftpClient.DirectoryExistsAsync(remotePath).Result;
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="remotePath">远程文件夹路径</param>
        public bool CreateDirectory(string remotePath)
        {
            string ss = ftpClient.CreateDirectoryAsync(remotePath).Result;
            if (string.IsNullOrEmpty(ss))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="filePath">本地文件名称</param>
        /// <param name="remotePath">远程文件名称</param>
        public void UpLoad(string filePath, string remotePath)
        {
            ftpClient.Upload(filePath, remotePath);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remotePath">远程文件名称</param>
        /// <param name="filePath">本地文件名称</param>
        public void Download(string remotePath, string filePath)
        {
            ftpClient.Download(remotePath,filePath);
        }
        /// <summary>
        /// 判断远程文件是否存在
        /// </summary>
        /// <param name="remotePath">远程文件名称</param>
        /// <returns></returns>
        public bool FileExists(string remotePath)
        {
            return ftpClient.FileExistsAsync(remotePath).Result;
        }
        /// <summary>
        /// 删除远程文件
        /// </summary>
        /// <param name="remotePath">远程文件名称</param>
        public void DeleteFile(string remotePath)
        {
            ftpClient.DeleteFileAsync(remotePath).Start();
        }
        /// <summary>
        /// 删除远程文件夹
        /// </summary>
        /// <param name="remotePath">远程文件夹名称</param>
        public void DeleteDirectory(string remotePath)
        {
            ftpClient.RemoveDirectoryAsync(remotePath).Start();
        }
        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="fromPath">要更名的远程文件或目录的路径名</param>
        /// <param name="toPath">要重命名文件或目录的路径名</param>
        public void Rename(string fromPath, string toPath)
        {
            ftpClient.RenameAsync(fromPath, toPath).Start();
        }
        /// <summary>
        /// FTP关闭
        /// </summary>
        public void Close()
        {
            ftpClient.Disconnect();
        }
    }
}
